package com.javacoo.junit.generator.config;

import java.util.ArrayList;
import java.util.List;

import com.javacoo.junit.generator.exception.InvalidConfigurationException;

import static com.javacoo.junit.generator.internal.util.StringUtility.stringHasValue;
import static com.javacoo.junit.generator.internal.util.messages.Messages.getString;
/**
 * 配置对象
 * <li></li>
 * @author duanyong@jccfc.com
 * @date 2020/12/31 16:34
 */
public class Configuration {

    private final List<Context> contexts;

    private final List<String> classPathEntries;

    public Configuration() {
        super();
        contexts = new ArrayList<>();
        classPathEntries = new ArrayList<>();
    }

    public void addClasspathEntry(String entry) {
        classPathEntries.add(entry);
    }

    public List<String> getClassPathEntries() {
        return classPathEntries;
    }

    /**
     * 校验
     * <li>这个方法做了一个简单的验证，它确保所有必需的字段都已填写并且实现类存在并且是正确的类型。它不执行任何更复杂的操作，例如：验证数据库表是否存在或命名列是否存在</li>
     * @author duanyong@jccfc.com
     * @date 2020/12/31 16:33
     * @return: void
     */
    public void validate() throws InvalidConfigurationException {
        List<String> errors = new ArrayList<>();

        for (String classPathEntry : classPathEntries) {
            if (!stringHasValue(classPathEntry)) {
                errors.add(getString("ValidationError.19"));
                // only need to state this error once
                break;
            }
        }

        if (contexts.isEmpty()) {
            errors.add(getString("ValidationError.11"));
        } else {
            for (Context context : contexts) {
                context.validate(errors);
            }
        }

        if (!errors.isEmpty()) {
            throw new InvalidConfigurationException(errors);
        }
    }

    public List<Context> getContexts() {
        return contexts;
    }

    public void addContext(Context context) {
        contexts.add(context);
    }
}
